const { updateDocument, getDocument } = require("./mongoose.js")
const mongoose = require('mongoose')
const { uri, options } = require('./mongodb.config')
const { WeChat_Document } = require('./schema')
//mongoose操作数据库入口文件
//创建连接
mongoose.connect(uri, options)
    .then(() => {
        console.log('连接成功')
    }).catch(err => {
        console.log(err)
        console.log('连接失败')
    })

const query = {
    condition: {},
    limit: 999,
    skip: 0
}
const getRandom = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)


function getDocumentFunc() {
    getDocument(query).then(async (res) => {
        const length = res.res.length
        const data = res.res

        for (let i = 1; i <= 1; i++) {
            // console.log(data[i-1])
            // let id = data[i-1].id
            let id = data[getRandom(0, length - 1)].id
            // // let to = '/document/sleep/'+ id
            let isHot = true
            let condition = {
                $set: {
                    isHot
                }
            }

            const res = await updateDocument(id, condition)
            console.log(res)
            console.log('ok===' + id)
        }
    })
}

(function add() {
    WeChat_Document.find({}).then(async (res) => {
        let num = 1
        for (let i = 0; i < res.length; i++) {
            const result = await WeChat_Document.updateOne({ name: res[i].name }, { id: 'WX-' + (num++).toString().padStart(3, '0')}, { upsert: true })
            console.log(result)
        }
    })
    // WeChat_Document.updateMany({},{to: '/news', date: Date.now()}, {upsert: true}).then(res=> {
    //     console.log(res)
    // })
})()

